/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.generator.match;

import com.farmcode.console.api.model.MatchContext;

import com.farmcode.base.web.BaseBacking;
import com.farmcode.console.api.business.custom.ExtMatchContextManager;
import com.farmcode.console.api.business.custom.ExtTemplateManager;
import com.farmcode.console.api.model.Match;
import com.farmcode.console.api.model.PackageTemplate;
import com.farmcode.console.api.model.Template;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

/**
 *
 * @author Oscar Ivan Prado
 */
@Named(value = "matchContextBackingBean")
@RequestScoped
public class MatchContextBackingBean extends BaseBacking{
    
    @EJB
    private ExtMatchContextManager extMatchContextManager;
    @EJB
    private ExtTemplateManager extTemplateManager;
    
    private MatchContext newMatchContext;
    private List<MatchContext> matchContexts;
    private List<Template> templates;
    private Long templateId;
    /** Creates a new instance of MatchContextBackingBean */
    public MatchContextBackingBean() {
    }

    public void onSave(){
        MatchContext mc = getNewMatchContext();
        Match match = getSession().getValue(new Match().getClass().getCanonicalName());
        mc.setMatch(match);
        extMatchContextManager.create(mc);
        setNewMatchContext(null);
    }
    
    public void onRemove(MatchContext matchContext){
        extMatchContextManager.remove(matchContext);
    }
    
    public void onStore(MatchContext matchContext){
        extMatchContextManager.store(matchContext);
    }
    
    /**
     * @return the matchContexts
     */
    public List<MatchContext> getMatchContexts() {
        setMatchContexts(null);
        if(getSession().getValue(getClass().getCanonicalName() + ".matchContexts") != null){
            matchContexts = getSession().getValue(getClass().getCanonicalName() + ".matchContexts");
        }else{
            Match match = getSession().getValue(new Match().getClass().getCanonicalName());
            if(match != null && match.getId() != null){
                setMatchContexts(extMatchContextManager.getMatchContextByMatch(match));
            }
        }
        return matchContexts;
    }

    /**
     * @param matchContexts the matchContexts to set
     */
    public void setMatchContexts(List<MatchContext> matchContexts) {
        this.matchContexts = matchContexts;
        getSession().setValue(getClass().getCanonicalName() + ".matchContexts", matchContexts);
    }

    /**
     * @return the templates
     */
    public List<Template> getTemplates() {
        if(getSession().getValue(getClass().getCanonicalName() + ".templates") != null){
            templates = getSession().getValue(getClass().getCanonicalName() + ".templates");
        }else{
            PackageTemplate packageTemplate = getSession().getValue(new PackageTemplate().getClass().getCanonicalName());
            if(packageTemplate != null && packageTemplate.getId() != null){
                setTemplates(extTemplateManager.getTemplatesByPackage(packageTemplate));
            }
        }
        return templates;
    }

    /**
     * @param templates the templates to set
     */
    public void setTemplates(List<Template> templates) {
        this.templates = templates;
        getSession().setValue(getClass().getCanonicalName() + ".templates", templates);
    }

    /**
     * @return the templateId
     */
    public Long getTemplateId() {
        if(getSession().getValue("templateId") != null){
            templateId = getSession().getValue("templateId");
        }
            
        return templateId;
    }

    /**
     * @param templateId the templateId to set
     */
    public void setTemplateId(Long templateId) {
        this.templateId = templateId;
        getSession().setValue("templateId", templateId);
    }

    /**
     * @return the newMatchContext
     */
    public MatchContext getNewMatchContext() {
        MatchContext matchContext = new MatchContext();
        if(getSession().getValue(new MatchContext().getClass().getCanonicalName() + "_new") != null){
            newMatchContext = getSession().getValue(new MatchContext().getClass().getCanonicalName() + "_new");
        }else{
            setNewMatchContext(matchContext);
        }
        return newMatchContext;
    }

    /**
     * @param newMatchContext the newMatchContext to set
     */
    public void setNewMatchContext(MatchContext newMatchContext) {
        this.newMatchContext = newMatchContext;
        getSession().setValue(new MatchContext().getClass().getCanonicalName() + "_new", newMatchContext);
    }
    
    
}
